package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.dto.CreativeHomeDTO;
import com.example.springboot.entity.CCollection;
import com.example.springboot.entity.Creative;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.CCollectionMapper;
import com.example.springboot.service.ICCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.ICCommentService;
import com.example.springboot.service.ICreativeService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CCollectionServiceImpl extends ServiceImpl<CCollectionMapper, CCollection> implements ICCollectionService {

    @Resource
    private ICCollectionService collectionService;
    @Resource
    private ICreativeService creativeService;
    @Resource
    private ICCommentService commentService;

    @Override   //根据创意id查询收藏数
    public int getCount(Integer creativeId) {
        QueryWrapper<CCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creative_id",creativeId);
        return collectionService.list(queryWrapper).size();
    }

    @Override   //根据用户id查询收藏列表
    public List<CreativeHomeDTO> getByUser(Integer userId) {
        QueryWrapper<CCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<CCollection> cCollections = collectionService.list(queryWrapper);
        List<CreativeHomeDTO> creativeHomeDTOS = new ArrayList<>();
        for (CCollection cCollection : cCollections) {
            Creative creative = creativeService.getById(cCollection.getCreativeId());
            CreativeHomeDTO creativeHomeDTO = new CreativeHomeDTO();
            creativeHomeDTO.setId(creative.getId());
            creativeHomeDTO.setName(creative.getName());
            creativeHomeDTO.setFeature(Arrays.asList(creative.getFeature().split(" ")));
            if (creative.getDetail().length() <= 150)
                creativeHomeDTO.setDetail(creative.getDetail());
            else
                creativeHomeDTO.setDetail(creative.getDetail().substring(0,150) + "……");
            creativeHomeDTO.setView(creative.getView());
            creativeHomeDTO.setCollections(collectionService.getCount(creative.getId()));
            creativeHomeDTO.setComments(commentService.getCount(creative.getId()));
            creativeHomeDTO.setCreateTime(creative.getCreateTime().toLocalDate());

            creativeHomeDTOS.add(creativeHomeDTO);
        }
        creativeHomeDTOS.sort(Comparator.comparing(CreativeHomeDTO::getCreateTime).reversed());
        return creativeHomeDTOS;
    }

    @Override   //判断是否已有收藏关系
    public boolean judge(Integer userId, Integer creativeId) {
        if (userId == null || creativeId == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<CCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("creative_id",creativeId);
        CCollection cCollection;
        try {
            cCollection = collectionService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        return cCollection != null;
    }

    @Override
    public boolean removeByCreative(Integer creativeId) {
        QueryWrapper<CCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creative_id",creativeId);
        return collectionService.remove(queryWrapper);
    }

    @Override   //新增收藏关系
    public Result insertCollection(CCollection cCollection) {
        if (judge(cCollection.getUserId(),cCollection.getCreativeId()))
            return Result.error(Constants.CODE_600,"已有收藏关系");
        else {
            Creative creative;
            try {
                creative = creativeService.getById(cCollection.getCreativeId());
            }catch (Exception e) {
                throw new ServiceException(Constants.CODE_500,"系统错误");
            }
            if (creative == null)
                throw new ServiceException(Constants.CODE_600,"未找到创意");
            if (save(cCollection))
                return Result.success("收藏成功");
            else
                return Result.error(Constants.CODE_600,"收藏失败");
        }
    }

    @Override   //取消收藏
    public Result removeCollection(CCollection cCollection) {
        if (cCollection.getUserId() == null || cCollection.getCreativeId() == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<CCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cCollection.getUserId());
        queryWrapper.eq("creative_id",cCollection.getCreativeId());
        CCollection collection;
        Creative creative;
        try {
            collection = collectionService.getOne(queryWrapper);
            creative = creativeService.getById(cCollection.getCreativeId());
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (collection == null) {
            throw new ServiceException(Constants.CODE_600,"不存在收藏关系");
        }
        if (creative == null) {
            throw new ServiceException(Constants.CODE_600,"未找到创意");
        }
        if (remove(queryWrapper))
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"取消收藏失败");
    }
}
